package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import Boundary.UpdateStateTable;
import Visualization.StateObserverVis;

public class ShowStateTablePart 
{
	JPanel container;
	UpdateStateTable updateStateTable;
	ShowStateTablePart self=this;
	private JTable tab;
	
	public ShowStateTablePart(UpdateStateTable ust)
	{
		updateStateTable=ust;
	}
	
	public JPanel showStateTable()
	{
		container = new JPanel();
		container.setLayout(new BorderLayout());

		JLabel label = new JLabel("Show State Table part");
		label.setFont(new Font("Tahoma", Font.BOLD, 24));
		
		container.add(label, BorderLayout.NORTH);
		container.add(getCenterArea(), BorderLayout.CENTER);
		container.add(getEastArea(), BorderLayout.EAST);
		
		return container;
	}
	
	private JPanel getEastArea() 
	{
		JPanel pannellob= new JPanel();
		pannellob.setLayout(new GridLayout(7,1));
		
		JButton Choose = new JButton("Choose Used SO");
		JButton Split = new JButton("Split Cond-React");
		JButton State = new JButton("Set State");
		JButton Validate = new JButton("Validate");
		JButton Close = new JButton("Close");
		
		Choose.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				display(new DividiStateObservers(self).crea(updateStateTable.allSO));
			}
		});
		Split.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(updateStateTable.currentEVCR.getAll().isEmpty())
					showMessage("There are no Events");
				else updateStateTable.splitCR();
			}
		});
		State.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tab.getSelectedRow()!=-1)
				{String answer = JOptionPane.showInputDialog(container, 
				         "Insert new State's Name?", 
				         "", JOptionPane.INFORMATION_MESSAGE);
				if(answer!=null)if(answer.length()!=0) updateStateTable.defineState(updateStateTable.main.OPEN_PRJ.stateTable.getRow(tab.getSelectedRow()), answer);
			}}});
		Validate.addActionListener(new ActionListener() 
			{@Override public void actionPerformed(ActionEvent arg0) {updateStateTable.validateST();}});
		Close.addActionListener(new ActionListener() 
			{@Override public void actionPerformed(ActionEvent arg0) {updateStateTable.close();}});
		
		pannellob.add(new JLabel(""));
		pannellob.add(Choose);		
		pannellob.add(State);
		pannellob.add(Split);
		pannellob.add(Validate);
		pannellob.add(Close);
		pannellob.add(new JLabel(""));
		
		return pannellob;
	}

	@SuppressWarnings("serial")
	public JScrollPane getCenterArea()
	{
		JPanel tabella = new JPanel();
		
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		int rows = 0;
		int cols = 0;
		if(!updateStateTable.currentST.getUsedSo().getAll().isEmpty())
		{
			for (StateObserverVis so : updateStateTable.currentST.getUsedSo().getAll()) 
				modelloRicerca.addColumn(so.getName());
			cols=updateStateTable.currentST.getUsedSo().size();
			modelloRicerca.addColumn("State");
		}
		tab = new JTable(modelloRicerca){public boolean isCellEditable(int rowIndex, int vColIndex){return false;}};
		tab.setAutoCreateColumnsFromModel(false);								
		tab.getTableHeader().setReorderingAllowed(false);
		tab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tab.setFillsViewportHeight(true);
		
		if(!updateStateTable.currentST.getAllRows().getAll().isEmpty())
		{
			rows=updateStateTable.currentST.getAllRows().size();
			modelloRicerca.setRowCount(rows);				
				
			for (int i= 0; i<rows; i++)
			{
				for (int j=0; j<cols; j++)
					modelloRicerca.setValueAt(updateStateTable.currentST.getElementAt(i,j).getName(), i, j);
				modelloRicerca.setValueAt(updateStateTable.currentST.getAllRows().get(i).getName(), i, cols);
			}
				
			tabella.setLayout(new BorderLayout());
			tabella.add(tab.getTableHeader(), BorderLayout.NORTH);
			tabella.add(tab, BorderLayout.CENTER);
		}
		else tabella.add(new JLabel("<html><br><br><br><br><br><br>no State Observers Used</html>"));
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.getViewport().add(tabella);
		
		return scrollPane;
	}
	
	public void display(JPanel daVisualizzare)
	{
		container.removeAll();
		container.add(daVisualizzare);
		container.setVisible(true);
		container.revalidate();
	}
	
	public JPanel showSTP()
	{
		container.removeAll();
		container.add(showStateTable());
		container.setVisible(true);
		container.revalidate();
		
		return container;
	}

	public void showMessage(String string) 
	{
		JOptionPane.showMessageDialog(container, string, "Error", JOptionPane.WARNING_MESSAGE);
	}
}
